package com.linkage.jz.bankruptcy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.linkage.jz.bankruptcy.Consts;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.bean.Msg;
import com.linkage.jz.bankruptcy.utils.SPUtils;
import com.linkage.seapeak.pojo.MessageItem;
import com.seapeak.recyclebundle.BaseHolder;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import java.util.List;

import jp.wasabeef.glide.transformations.CropCircleTransformation;


/**
 * Created by Mr.sorrow on 2017/5/6.
 */

public class ChatAdapter extends BaseRecyclerAdapter<BaseHolder> {

    private LayoutInflater mLayoutInflater;
    private Context mContext;
    private List<MessageItem> mDatas;

    String sendUserId = SPUtils.getInstance(Consts.SPNAME).getString("userId","");

    public ChatAdapter(Context context, List<MessageItem> datas) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
        mDatas = datas;
    }

    @Override
    public BaseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            View view = mLayoutInflater.inflate(R.layout.item_chat_left, parent, false);
            return new ChatLeftViewHolder(view);
        } else {
            View view = mLayoutInflater.inflate(R.layout.item_chat_right, parent, false);
            return new ChatRightViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(BaseHolder holder, int position) {
        super.onBindViewHolder(holder, position);
        MessageItem msg = mDatas.get(position);
        String time = msg.getSendTime();
        String content = msg.getNote();
        if (holder instanceof ChatLeftViewHolder) {
            ((ChatLeftViewHolder) holder).mTvLeftTime.setText(time);
            ((ChatLeftViewHolder) holder).mTvMsgLeft.setText(content);
            Glide.with(mContext).load(msg.getSendUser().getAvatarUrl())
                    .bitmapTransform(new CropCircleTransformation(mContext))
                    .placeholder(R.drawable.logo)
                    .into(((ChatLeftViewHolder) holder).mAvatarView);
            ((ChatLeftViewHolder) holder).mTvMsgLeft.setText(content);
        } else if (holder instanceof ChatRightViewHolder) {
            ((ChatRightViewHolder) holder).mTvRightTime.setText(time);
            ((ChatRightViewHolder) holder).mTvMsgRight.setText(content);
            ((ChatRightViewHolder) holder).mTvMsgRight.setText(content);
            Glide.with(mContext).load(msg.getSendUser().getAvatarUrl())
                    .bitmapTransform(new CropCircleTransformation(mContext))
                    .placeholder(R.drawable.logo)
                    .into(((ChatRightViewHolder) holder).mAvatarView);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (mDatas != null && mDatas.get(position) != null && mDatas.get(position).getSendUserId() != null)
            return mDatas.get(position).getSendUserId().equals(sendUserId) ? 1 : 0;
        else
            return 0;
    }

    @Override
    public int getItemCount() {
        if (mDatas == null) {
            return 0;
        } else {
            return mDatas.size();
        }
    }

    static class ChatLeftViewHolder extends BaseHolder {

        TextView mTvLeftTime;
        TextView mTvMsgLeft;
        ImageView mAvatarView;

        ChatLeftViewHolder(View view) {

            super(view);
            mTvLeftTime = view.findViewById(R.id.tv_left_time);
            mTvMsgLeft = view.findViewById(R.id.tv_msg_left);
            mAvatarView = view.findViewById(R.id.img_ble);
        }
    }

    static class ChatRightViewHolder extends BaseHolder {

        TextView mTvRightTime;
        TextView mTvMsgRight;
        ImageView mAvatarView;

        ChatRightViewHolder(View view) {
            super(view);
            mTvRightTime = view.findViewById(R.id.tv_right_time);
            mTvMsgRight = view.findViewById(R.id.tv_msg_right);
            mAvatarView = view.findViewById(R.id.img_phone);
        }
    }
}
